package com.bocaizp.operation.controller.user;

import com.bocaizp.common.constant.JwtClaimsConstant;
import com.bocaizp.common.context.BaseContext;
import com.bocaizp.common.properties.JwtProperties;
import com.bocaizp.common.result.Result;
import com.bocaizp.common.utils.JwtUtil;
import com.bocaizp.operation.mapper.LoginMapper;
import com.bocaizp.operation.service.LoginService;
import com.bocaizp.pojo.VO.CityFlagVO;
import com.bocaizp.pojo.VO.IndustryDirectionVO;
import com.bocaizp.pojo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author : huang jie
 * Date : 2024/5/2 17:21
 **/
@RestController
@RequestMapping("/user")
@Slf4j
@CrossOrigin
public class LoginController {

	@Autowired
	private LoginService loginService;
	@Autowired
	private LoginMapper loginMapper;
	@Autowired
	private JwtProperties jwtProperties;

	/**
	 * 用户登录
	 * @param userdata
	 * @return
	 */
	@PostMapping("/login")
	public Result<User> userLogin(@RequestBody User userdata){
		log.info("用户登录请求... {},{}",userdata.getCardId(), userdata.getPassword());
		User user = loginService.login(userdata.getCardId(), userdata.getPassword());
		//登陆成功，封装返回给前端的数据
		Map<String, Object> claim = new HashMap<>();
		claim.put(JwtClaimsConstant.USER_ID, user.getId());
		//生成jwt令牌
		String token = JwtUtil.createJWT(
				jwtProperties.getUserSecretKey(),
				jwtProperties.getUserTtl(),
				claim);
		//封装对象
		user.setToken(token);
		//更改登录时间，增加登录次数
		LocalDateTime localDateTime= LocalDateTime.now();
		loginMapper.loginNumber(user.getId(), user.getLoginNumber() + 1 , localDateTime);
		return Result.success(user);
	}
	@GetMapping("/test")
	public Result<String> nametest(HttpSession session){
		log.info("获取用户名请求...");
		Integer user = Math.toIntExact(BaseContext.getCurrentId());
		System.out.println(user);
		if (user != 0) {
			return Result.success(user.toString());
		} else {
			// 用户未登录或session失效，返回相应的失败结果
			return Result.error("用户未登录或session已失效");
		}
	}

	/**
	 * 用户注册
	 * @param user
	 * @return
	 */
	@PostMapping("/register")
	public Result userRegister(@RequestBody User user){
		log.info("用户注册请求...");
		loginService.insert(user);
		return Result.success();
	}

	/**
	 * 找回密码
	 * @param user
	 * @return
	 */
	@PostMapping("/backpd")
	public Result<String> userBack(@RequestBody User user){
		log.info("找回密码请求...");
		String password = loginService.reback(user.getUsername(), user.getCardId());
		return Result.success(password);
	}

	/**
	 * 获取城市列表
	 * @return
	 */
	@GetMapping("/getCity")
	public Result<List<CityFlagVO>> getCity(){
		log.info("获取城市列表请求...");
		List<CityFlagVO> cityFlagVO = loginService.getCity();
		return Result.success(cityFlagVO);
	}

	/**
	 * 获取职位分类
	 * @param industry
	 * @return
	 */
	@GetMapping("/guide/industry")
	public Result<List<IndustryDirectionVO>> getDirection(String industry){
		log.info("获取职位分类请求...{}",industry);
		List<IndustryDirectionVO> industryDirectionVOList = loginService.getDirection(industry);
		return Result.success(industryDirectionVOList);
	}

	/**
	 * 设置用户意向信息
	 * @param user
	 * @return
	 */
	@PostMapping("/submit")
	public Result submit(@RequestBody User user){
		log.info("设置用户基本信息...");
		loginService.update(user);
		return Result.success();
	}

	/**
	 * 获取用户基本信息
	 * @return
	 */
	@PostMapping("/getInfo")
	public Result<User> geyInfo(){
		log.info("获取用户基本信息...");
		User user = loginService.getInfo();
		return Result.success(user);
	}


}
